Turns the order of the places in target around, in place: the first place takes the value the last one held and the other way round, all the way to the middle. It changes target itself and makes no copy. Any kind of value can be reversed, text included.
array_reverse(target)| Name | Type |
|---|---|
target | array |
Nothing.
Turns the order of the places in target, an array, around. The first place takes the value the last one held, the last takes the first one's, and the ones in between swap the same way, all the way to the middle.
array_reverse(order); // 1, 2, 3, 4 becomes 4, 3, 2, 1array_reverse moves the values that are already in target. It does not make a new array, and it needs no extra memory. The old order is gone afterwards. If you want to keep it, make a copy with array_copy first and reverse the one you want turned around.
It turns around the places the array really has, the number array_length gives you. An array never gains or loses a place, so it has the same places afterwards.
Any kind of value can be reversed: numbers, text, structs, references. Nothing is compared, so there is no rule about what the array holds.
| You do this | What happens |
|---|---|
| reverse an array with one place | nothing changes |
| reverse an array that has the same value more than once | works as normal. Equal values are interchangeable, so you cannot tell which is which |
| reverse it twice | you are back where you started |
| give it a list | refused when checked. A list has its own, list_reverse |
| use it where a value is needed | refused when checked. It gives nothing back |
A script that takes an array as a parameter works on the caller's own array (see Arrays). So array_reverse inside a script reverses the caller's array, whatever its size.
Nothing is set aside: the values are swapped where they stand, one swap for every two places. No timing was measured.
var order = array_create(4, 0);
order[0] = 1;
order[1] = 2;
order[2] = 3;
order[3] = 4;
array_reverse(order); // order is now 4, 3, 2, 1
var first = order[0]; // 4Reverses the array_length(target) places the array has. Reverse (the GBA module's own alias for std::reverse): it swaps the first item with the last, the second with the second-last, and so on towards the middle, in place - no copy and no extra memory, O(n) in how many items it turns around. Nothing is compared, so any kind of item works, text and structs included. With no items or one item there is nothing to swap and nothing happens.